<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改角色</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="css/layui.css" rel="stylesheet">
</head>
<body>
  <div style="width:90%;margin-top: 20px;">
    <form class="layui-form" lay-filter="role">
      <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block">
          <select id="roleList" name="systemRoleId" lay-verify="required" lay-reqText="请选择角色" lay-search="">
            <option value="">请选择角色或搜索</option>
          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <div class="layui-input-inline">
          <button class="layui-btn" id="btnSubmit">确定</button>
        </div>
      </div>
    </form>
  </div>
<script src="js/layui.js"></script>
<script type="module">
  import userManager from './js/user-manager.js'
  import roleManager from './js/role-manager.js'

  function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
      return unescape(r[2]);
    }

    return null;
  }

  layui.use(['form','layer'], function(){
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var id = getQueryString('id');
    var systemRoleId=getQueryString('systemRoleId');
    console.log(id + ',' + systemRoleId)
    // 加载角色列表
    var loadRoleList = function(){
        roleManager.getRoleList($,function(rspData){
            $.each(rspData.data,function(idx,role){
              if(role.id == parseInt(systemRoleId)){
                $('#roleList').append('<option value="'+role.id+'" selected>'+role.name+'</option>');
              } else {
                $('#roleList').append('<option value="'+role.id+'">'+role.name+'</option>');
              }
            });
            form.render();
        })
    }
    loadRoleList();
    $('#btnSubmit').on('click', function(){
      form.submit('role', function(data){
        var user = { id : id, systemRoleId : data.field.systemRoleId};
            userManager.updateUserRole($,user,function(rspData){
              if(rspData.code !=1){
                layer.msg("修改失败!",{icon:5});
              } else {
                layer.msg("修改成功");
                setTimeout(() => {
                  var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                  parent.layer.close(index); //再执行关闭 
                  parent.location.reload(); // 刷新页面
                }, 500);
              }
            })
      });

      return false;
    });
  });
</script>
</body>
</html>